package com.lixin.smartclassroom.ui.home.tab1.fragment

import android.view.LayoutInflater
import android.view.ViewGroup
import com.drake.brv.utils.bindingAdapter
import com.drake.channel.receiveEvent
import com.drake.net.utils.scopeNetLife
import com.lixin.smartclassroom.R
import com.lixin.smartclassroom.api.classhistorylist
import com.lixin.smartclassroom.api.classpiyuelist
import com.lixin.smartclassroom.api.classstudent
import com.lixin.smartclassroom.api.classteacher
import com.lixin.smartclassroom.api.classzhktlist
import com.lixin.smartclassroom.api.classzhuticourselist
import com.lixin.smartclassroom.api.courselist
import com.lixin.smartclassroom.api.deletejingcai
import com.lixin.smartclassroom.api.huodongguanli
import com.lixin.smartclassroom.api.huodongyanxue
import com.lixin.smartclassroom.api.huodongzhuti
import com.lixin.smartclassroom.api.jingcaishikelist
import com.lixin.smartclassroom.api.paperpiyue
import com.lixin.smartclassroom.api.zhkthuodong
import com.lixin.smartclassroom.bean.DataListBean
import com.lixin.smartclassroom.databinding.FragmentRecyclerCustomBinding
import com.lixin.smartclassroom.ext.child_id
import com.lixin.smartclassroom.ui.home.adapter.classNotice
import com.lixin.smartclassroom.ui.home.adapter.classStudent
import com.lixin.smartclassroom.ui.home.adapter.classTeacher
import com.lixin.smartclassroom.ui.home.adapter.classVenueTasks
import com.lixin.smartclassroom.ui.home.adapter.courseManager
import com.lixin.smartclassroom.ui.home.adapter.courseMyAttend
import com.lixin.smartclassroom.ui.home.adapter.courseReview
import com.lixin.smartclassroom.ui.home.adapter.excitingMomentManager
import com.lixin.smartclassroom.ui.home.adapter.yanXueCenter
import com.lixin.smartclassroom.ui.home.tab1.activity.CommentDetailAct
import com.lixin.smartclassroom.ui.home.tab2.activity.CourseReviewCommentAct
import com.lixin.smartclassroom.ui.home.tab2.activity.SmartCourseDetailAct
import com.lixin.smartclassroom.ui.home.tab5.activity.EventCommentAct
import com.lixinkeji.base.BaseFragment
import com.lixinkeji.extension.getIntArgument
import com.lixinkeji.extension.getStringArgument
import com.lixinkeji.extension.gone
import com.lixinkeji.extension.removeAt
import com.lixinkeji.extension.start
import com.lixinkeji.extension.visible

/**
 * Description 班级老师
 * Author: lwc
 * Date:2023/12/9 15:35
 */
class ClassDetailChildFra : BaseFragment<FragmentRecyclerCustomBinding>() {
    override fun getViewBinding(
        inflater: LayoutInflater, container: ViewGroup?
    ) = FragmentRecyclerCustomBinding.inflate(inflater)

    private var listBean = ArrayList<DataListBean>()
    private var type =
        0 //1智慧任务  2场馆任务  3班级学生 4班级通知 5班级老师 6待批阅 7研学活动  8图片  9视频  10已批阅   11待批阅班级  12已批阅班级
    private var fromAct = 0 //1活动管理  2班级详情
    private lateinit var classid: String
    private lateinit var courseid: String

    override fun iniView() {
        classid = getStringArgument("classid")
        courseid = getStringArgument("courseid")
        type = getIntArgument("type")
        fromAct = getIntArgument("fromAct")

        receiveEvent<String>("refreshList") {
            binding.refresh.refresh()
        }

        when (type) {
            1 -> {
                when (fromAct) {
                    1 -> {
                        binding.recycler.courseManager("1").apply {
                            R.id.rl.onClick {
                                start(EventCommentAct::class.java){
                                    putExtra("courseid",getModel<DataListBean>(modelPosition).id)
                                }
                            }
                        }
                    }

                    else -> {
                        binding.recycler.courseMyAttend(2).apply {
                            R.id.rl.onClick {
                                start(SmartCourseDetailAct::class.java) {
                                    putExtra("type", "1")
                                    putExtra(
                                        "courseid",
                                        (getModel(modelPosition) as DataListBean).id
                                    )
                                }
                            }
                        }
                    }
                }
            }

            2 -> {
                when (fromAct) {
                    1 -> {
                        binding.recycler.courseManager("2").apply {
                            R.id.rl.onClick {
                                start(EventCommentAct::class.java){
                                    putExtra("courseid",getModel<DataListBean>(modelPosition).id)
                                }
                            }
                        }
                    }

                    else -> {
                        binding.recycler.classVenueTasks(2).apply {
                            R.id.rl.onClick {
                                start(SmartCourseDetailAct::class.java) {
                                    putExtra("type", "2")
                                    putExtra(
                                        "courseid",
                                        (getModel(modelPosition) as DataListBean).id
                                    )
                                }
                            }
                        }
                    }
                }
            }


            3 -> binding.recycler.classStudent()
            4 -> binding.recycler.classNotice(classid)
            5 -> binding.recycler.classTeacher()
            6 -> binding.recycler.courseReview().apply {
                R.id.rl.onClick {
                    start(CommentDetailAct::class.java) {
                        putExtra(
                            "xiaojieid",
                            (models?.get(modelPosition) as DataListBean).xiaojieid
                        )
                        putExtra("id", (models?.get(modelPosition) as DataListBean).id)
                        putExtra("type", "2")


                    }
                }
            }
            10->{
                binding.recycler.courseReview().apply {
                    R.id.rl.onClick {
                        start(CommentDetailAct::class.java) {
                            putExtra(
                                "xiaojieid",
                                (models?.get(modelPosition) as DataListBean).xiaojieid
                            )
                            putExtra("id", (models?.get(modelPosition) as DataListBean).id)
                            putExtra("type", "2")
                            putExtra("fromAct", "4")


                        }
                    }
                }
            }

            11, 12 -> binding.recycler.courseReview().apply {
                R.id.rl.onClick {
                    start(CommentDetailAct::class.java) {
                        putExtra(
                            "xiaojieid",
                            (models?.get(modelPosition) as DataListBean).xiaojieid
                        )
                        putExtra("id", (models?.get(modelPosition) as DataListBean).id)
                        putExtra("type", "2")
                    }
                }
            }

            7 -> {
                when (fromAct) {
                    1 -> {
                        binding.recycler.courseManager("3").apply {
                            R.id.rl.onClick {
                                start(EventCommentAct::class.java){
                                    putExtra("courseid",getModel<DataListBean>(modelPosition).id)
                                }
                            }
                        }
                    }

                    else -> {
                        binding.recycler.yanXueCenter().apply {
                            R.id.rl.onClick {
                                if (fromAct == 1) {
                                    start(EventCommentAct::class.java){
                                        putExtra("courseid",getModel<DataListBean>(modelPosition).id)
                                    }
                                }
                            }
                        }
                    }
                }
            }




            8 -> binding.recycler.excitingMomentManager().apply {
                R.id.btnDelete.onClick {
                    scopeNetLife {
                        deletejingcai((models?.get(modelPosition) as DataListBean).id!!)
                        binding.recycler.bindingAdapter.removeAt(modelPosition)

                    }
                }
            }

            9 -> binding.recycler.excitingMomentManager().apply {
                R.id.btnDelete.onClick {
                    scopeNetLife {
                        deletejingcai((models?.get(modelPosition) as DataListBean).id!!)
                        binding.recycler.bindingAdapter.removeAt(modelPosition)

                    }

                }
            }
        }



        binding.apply {
            refresh.onRefresh {
                scopeNetLife {
                    when (type) {
                        1 -> {
                            if (fromAct == 0)
                                listBean = courselist(index, 10, "2").dataList!!
                            else if (fromAct == 1)
                                listBean = zhkthuodong()!!
                            else
                                listBean = classzhktlist(index, 10, classid,requireContext().child_id)!!
                        }

                        2 -> {
                            if (fromAct == 0)
                                listBean = courselist(index, 10, "2").dataList!!
                            else if (fromAct == 1)
                                listBean = huodongzhuti()!!
                            else
                                listBean = classzhuticourselist(index, 10, classid,requireContext().child_id)!!
                        }

                        3 -> listBean = classstudent(index, 10, classid, "1")!!
                        4 -> listBean = classhistorylist(classid, index, 10)!!
                        5 -> listBean = classstudent(index, 10, classid, "2")!!
                        6 -> listBean = paperpiyue(courseid, "1", index, 10)!!
                        10 -> listBean = paperpiyue(courseid, "3", index, 10)!!
                        11 -> listBean = classpiyuelist(classid, "1", index, 10)!!
                        12 -> listBean = classpiyuelist(classid, "3", index, 10)!!
                        7 -> {
                            if (fromAct == 0)
                                listBean = courselist(index, 10, "2").dataList!!
                            else
                                listBean = huodongyanxue()!!
                        }

                        8 -> listBean = jingcaishikelist(index, 10, "1")!!
                        9 -> listBean = jingcaishikelist(index, 10, "2")!!
                    }

                    addData(listBean) {
                        if (binding.recycler.bindingAdapter.models?.size == 0) {
                            binding.state.showEmpty()
                            binding.recycler.gone()
                        } else {
                            binding.recycler.visible()
                        }
                        listBean.size == 10
                    }
                }
            }.autoRefresh()
        }
    }

    override fun onResume() {
        super.onResume()
        binding.refresh.autoRefresh()
    }

    override fun onClick() {

    }

}